package controller;

import java.util.Calendar;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import config.model.Announcement;
import config.model.Project;
import interceptor.AdminLoginIntercept;
import service.AnnouncementService;
import service.ChangeService;
import service.ExcelService;
import service.LoginService;
import service.ProjectService;


/**
 * 管理员控制类
 * 
 * @author 陈凌志
 *
 */

@Before(AdminLoginIntercept.class)
public class AdminController extends Controller{

	AnnouncementService as = new AnnouncementService();
	ProjectService ps = new ProjectService();
	ChangeService cs = new ChangeService();
	ExcelService es = new ExcelService();
	/**
	 * 访问管理员主页面方法
	 */
	public void index() {
		setSessionAttr("thisYear", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		setAttr("title","公告信息");
		setAttr("announcementPage",as.queryAnnouncementPage(pageNumber));
		render("index.html");
	}
	
	/**
	 * 此方法用于添加公告，公告文件存放于file/announcement文件下，文件名为上传时的文件名
	 * 
	 */
	public void addAnnouncement() {
		UploadFile file=getFile("file","announcement",1024*1024*1024,"utf-8");
		Announcement ann = getModel(Announcement.class);
		if(file!=null) 
			ann.setFile(file.getOriginalFileName());
		as.addAnnouncement(ann);
		redirect("/admin/index?pageNumber=1");
		
	}
	
	/**
	 * 此方法用于删除公告
	 * 
	 */
	public void deleteAnnouncement() {
		as.deleteAnnouncement(getPara("id"));
		redirect("/admin/index?pageNumber=1");
	}
	
	/**
	 * 此方法用于导入用户的页面访问
	 * 
	 */
	public void addUserPage() {
		setAttr("title","添加用户");
		render("addUsers.html");
		
	}
	
	/**
	 * 此方法用于导入excel的账号
	 * 
	 */
	public void addUsers() {
		UploadFile file=getFile("file","users",1024*1024*1024,"utf-8");
		setAttr("title",es.readExcel(file.getFile()));
		render("success.html");
		
	}
	
	/**
	 * 此方法用于立项通过页面访问
	 * 
	 */
	public void  approvalProject() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String projectYear = getSessionAttr("thisYear");
		setAttr("projectPage",ps.queryProjectByStatus("待审核立项",pageNumber,projectYear));
		setAttr("file",ps.getFileName(ps.queryProjectByStatus("待审核立项",pageNumber,projectYear).getList(),"application"));
		setAttr("endfile",ps.getFileName(ps.queryProjectByStatus("待审核立项",pageNumber,projectYear).getList(),"end"));
		setAttr("year",ps.getProjectYear());
		setAttr("selectYear",projectYear);
		setAttr("title","立项通过审核");
		render("approvalProject.html");
	}
	
	/**
	 * 此方法用于立项通过
	 * 
	 */
	public void  pass() {
		/*String projectId = getPara("projectId");
		String newProjectId = getPara("newProjectId");
		String year = getPara("year");
		if(ps.queryProjectByProjectId(projectId) != null) {
			if(ps.pass(projectId, newProjectId,year)) {
				setAttr("title","立项通过成功");
				render("success.html");
			}else {
				setAttr("title","立项通过失败");
				render("success.html");
			}
		}else {
			setAttr("title","项目ID已存在");
			render("success.html");
		}*/
		String key = "立项通过成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				String newID = getPara(a[i]);
				String year = getPara(a[i]+"year");
				if(!newID.isEmpty()) {
					ps.pass(a[i], newID,year);
				}else {
					key = "某个项目没有填写项目编号，填写过的立项成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何项目");
			render("success.html");
		}
		
		
	}
	
	/**
	 * 此方法用于搜索时的模糊查询
	 * 
	 */
	public void fuzzyQuery() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String key = getPara("key");
		Page<Record> project = ps.fuzzyQuery(key, pageNumber);
		setAttr("title","项目信息");
		setAttr("project",project);
		setAttr("file",ps.getFileName(project.getList(),"application"));
		setAttr("endfile",ps.getFileName(project.getList(),"end"));
		render("projectInfo.html");
	}
	
	
	/**
	 * 项目信息页面访问方法
	 * 
	 */
	public void ProjectInfo () {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		Page<Record> project = ps.queryAllProject(pageNumber);
		setAttr("title","项目信息");
		setAttr("project",project);
		setAttr("file",ps.getFileName(project.getList(),"application"));
		setAttr("endfile",ps.getFileName(project.getList(),"end"));
		render("projectInfo.html");
	}
	
	
	/**
	 * 人员变更审核页面访问方法
	 * 
	 */
	public void changePeople () {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		Page<Record> change = cs.queryChangePeople(pageNumber);
		setAttr("title","人员变更审核");
		setAttr("change",change);
		render("changePeople.html");
	}
	
	/**
	 * 人员变更审核通过方法
	 * 
	 */
	public void changePeopleYes() {
		String projectId = getPara("projectId");
		String type = getPara("type");
		if(cs.changPeople(projectId, "通过", type)) {
			setAttr("title","已通过该申请");
			render("success.html");
		}else {
			setAttr("title","通过失败");
			render("success.html");
		}
	}
	
	/**
	 * 人员变更审核拒绝方法
	 * 
	 */
	
	
	public void changePeopleNo() {
		String projectId = getPara("#projectId");
		String type = getPara("type");
		if(cs.changPeople(projectId, "拒绝", type)) {
			setAttr("title","已拒绝该申请");
			render("success.html");
		}else {
			setAttr("title","拒绝失败");
			render("success.html");
		}
	}
	

	/**
	 * 此方法用于结项通过页面访问
	 * 
	 */
	public void  end() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String projectYear = getPara("projectYear");
		setAttr("projectPage",ps.queryProjectByStatus("待审核结项",pageNumber,projectYear));
		setAttr("endfile",ps.getFileName(ps.queryProjectByStatus("待审核结项",pageNumber,projectYear).getList(),"end"));
		setAttr("year",ps.getProjectYear());
		setAttr("selectYear",projectYear);
		setAttr("title","结项通过审核");
		render("end.html");
	}
	
	
	/**
	 * 此方法用于结项通过
	 * 
	 */
	public void  passEnd() {
		
		String key = "结项通过成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				if(ps.End("结项", a[i])) {
					
				}else {
					key = "某个项目没有结项成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何项目");
			render("success.html");
		}
		
		
	}
	
	/**
	 * 此方法用于报销通过页面访问
	 * 
	 */
	public void  claim() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String projectYear = getPara("projectYear");
		setAttr("projectPage",ps.queryClaim(pageNumber, projectYear));
		setAttr("claimfile",ps.getFileName(ps.queryClaim(pageNumber,projectYear).getList(),"claim"));
		setAttr("year",ps.getProjectYear());
		setAttr("selectYear",projectYear);
		setAttr("title","报销通过审核");
		render("claim.html");
	}
	
	/**
	 * 此方法用于报销通过
	 * 
	 */
	public void  passClaim() {
		
		String key = "报销通过成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				if(ps.claim(a[i])) {
					
				}else {
					key = "某个项目没有报销成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何项目");
			render("success.html");
		}
		
		
	}
	
	/**
	 * 此方法用于项目分组页面访问
	 * 
	 */
	public void  groupProject() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		String projectYear = getPara("projectYear");
		setAttr("projectPage",ps.queryProjectByStatusAndGroup("待审核立项",pageNumber,projectYear));
		setAttr("file",ps.getFileName(ps.queryProjectByStatusAndGroup("待审核立项",pageNumber,projectYear).getList(),"application"));
		setAttr("year",ps.getProjectYear());
		setAttr("selectYear",projectYear);
		setAttr("title","项目分组");
		render("groupProject.html");
	}
	
	/**
	 * 此方法用于项目分组
	 * 
	 */
	public void  addGroupProject() {
		String key = "分组成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				String group = getPara(a[i]+"group");
				if(!group.isEmpty()) {
					if(ps.updateGroup(a[i], group)) {
						
					}else {
						key = "某个项目没有成功";
					}
				}else {
					key = "某个项目没有填写组号，填写过的成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何项目");
			render("success.html");
		}
		
		
	}
	
	/**
	 * 此方法用于评委分组页面访问
	 * 
	 */
	public void  groupTeacher() {
		LoginService ls = new LoginService();
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		setAttr("teacherPage",ls.queryTeacher(pageNumber));
		setAttr("title","评委分组");
		render("teacherGroup.html");
	}
	
	/**
	 * 此方法用于评委分组
	 * 
	 */
	public void  addGroupTeacher() {
		LoginService ls = new LoginService();
		String key = "分组成功";
		String [] a = getParaValues("boxes");
		if(a!=null) {
			for(int i=0;i<a.length;i++) {
				String group = getPara(a[i]+"group");
				if(!group.isEmpty()) {
					if(ls.updateGroup(a[i], group)) {
						
					}else {
						key = "某个没有成功";
					}
				}else {
					key = "某个没有填写组号，填写过的成功";
				} 
			}
			setAttr("title",key);
			render("success.html");
		}else {
			setAttr("title","你没有选择任何评委");
			render("success.html");
		}
		
		
	}
	
	/**
	 * 更改项目信息页面访问方法
	 * 
	 */
	public void changeProject () {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		Page<Record> project = ps.queryAllProject(pageNumber);
		setAttr("title","更改项目信息");
		setAttr("project",project);
		render("changeProject.html");
	}
	/**
	 * 更改项目信息方法
	 * 
	 */
	public void changeProjectInfo () {
		Project p = getModel(Project.class);
		if(ps.changeProject(p)) {
			setAttr("title","更改成功");
			render("success.html");
		}else {
			setAttr("title","更改失败");
			render("success.html");
		}
	}
	
	/**
	 * 导出excel列表页面访问方法
	 * 
	 */
	
	public void excelList() {
		render("excelList.html");
	}
	
	/**
	 * 导出某个特定excel页面的访问方法
	 * 
	 */
	
	public void excel() {
		String type = getPara("type");
		setAttr("tableName","type");
		setAttr("thead",es.getThead(type));
		setAttr("tbody",es.getTbody(type));
		render("excel.html");
	}
	/**
	 * 退出登录方法
	 * 
	 */
	public void signOut() {
		removeSessionAttr("User");
		redirect("/index");
	}

}
